//
//  StatusPictureView.swift
//  WeiBo_Private
//
//  Created by Yangperlove on 16/1/18.
//  Copyright © 2016年 Yangperlove. All rights reserved.
//
//
import UIKit
import SnapKit
import SDWebImage

let identifier = "PictureViewCell"

class StatusPictureView: UICollectionView {
    
    var imageUrls: [NSURL]? {
        didSet {
            //计算配图视图的大小
            let pSize = caclePictureViewSize()
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(pSize)
//            testLabel.text = "\(imageUrls?.count)"
                
            reloadData()
            }
        }
    }

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.minimumInteritemSpacing = pictureCellMargin
        flowLayout.minimumLineSpacing = pictureCellMargin
        flowLayout.scrollDirection = .Horizontal
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
        backgroundColor = UIColor.whiteColor()
        
        setupUI()
    }
    
    func setupUI() {
        registerClass(PictureViewCell.self, forCellWithReuseIdentifier: identifier)
        self.dataSource = self
//        self.reloadData()
//        addSubview(testLabel)
//        
//        testLabel.snp_makeConstraints { (make) -> Void in
//            make.center.equalTo(self.snp_center)
//        }
    
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //计算配图视图的大小
    func caclePictureViewSize() -> CGSize {
        let imageCount = imageUrls?.count ?? 0
        let flowLayout = self.collectionViewLayout as! UICollectionViewFlowLayout
        //无图
        if imageCount == 0 {
            return CGSizeZero
        }
        //单图
        if imageCount == 1 {
            let itemSize = CGSize(width: 200, height: 200)
            flowLayout.itemSize = itemSize
            return itemSize
        }
        
        let rowNumber = CGFloat((imageCount - 1) / 3 + 1)
//        let columnNumber = CGFloat((imageCount - 1) % 3 + 1)
        let rowWidth = (screenSize.width - 2 * kStatusCellMargin - 2 * pictureCellMargin)/3
        
        let itemSize =  CGSize(width: rowWidth, height: rowWidth)
        flowLayout.itemSize = itemSize

        //4张图
        if imageUrls!.count == 4 {
            return CGSize(width: 2 * itemSize.width + pictureCellMargin, height: 2 * itemSize.width + pictureCellMargin)
        }
//        flowLayout.itemSize = CGSize(width: rowWidth, height: rowWidth)
        let maxH = rowNumber * rowWidth + (rowNumber - 1) * pictureCellMargin
//        let maxW = columnNumber * rowWidth + (columnNumber - 1) * pictureCellMargin
        
        return CGSize(width: rowWidth * 3 + 2 * pictureCellMargin , height: maxH)
    }
    
    //MARK: 懒加载子视图
//    private lazy var testLabel: UILabel = UILabel(title: nil, color: UIColor.redColor(), fontSize: 20, margin: 0)
}

extension StatusPictureView: UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageUrls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(identifier, forIndexPath: indexPath) as! PictureViewCell
        cell.imgURL = imageUrls![indexPath.item]
        
        return cell
    }
}

class PictureViewCell: UICollectionViewCell {
    var imgURL: NSURL? {
        didSet {
            iconView.sd_setImageWithURL(imgURL)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        contentView.addSubview(iconView)
        
        iconView.snp_makeConstraints { (make) -> Void in
            //设置边界重合,即大小一样
            make.edges.equalTo(contentView.snp_edges)
        }
    }
    
    lazy var iconView: UIImageView = {
        var icon = UIImageView()
        icon.contentMode = .ScaleAspectFill
        icon.clipsToBounds = true
        return icon
    }()
    
    
    
}
